package com.gx.mvp.base;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.gx.mvp.rx.RxManager;
import com.gx.mvp.widget.CustomLoadDialog;

/**
 * Created by Guoxin on 2019/12/05.
 */
public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements View.OnClickListener, BaseView {

    protected View mRootView;

    protected P mPresenter;

    protected RxManager mRxManager = new RxManager();

    protected Context mContext;

    private CustomLoadDialog mLoadingDialog;

    private boolean mIsViewPrepared;    // 标识fragment视图已经初始化完毕
    private boolean mHasLoadData;       // 标识已经触发过懒加载数据

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.mRootView = inflater.inflate(this.getContentViewId(), container, false);
        // 创建加载框
        this.mLoadingDialog = new CustomLoadDialog(mContext);
        this.mLoadingDialog.setCanceledOnTouchOutside(false);
        this.initView();
        this.initListener();
        return this.mRootView;
    }

    protected final <T extends View> T findViewById(int id) {
        return mRootView.findViewById(id);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        this.mPresenter = this.createPresenter();
        this.mIsViewPrepared = true;
        this.initData();
        this.lazyLoadData();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            this.lazyLoadData();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (this.mRootView != null && this.mRootView.getParent() != null) {
            ((ViewGroup) this.mRootView.getParent()).removeView(this.mRootView);
        }
        this.mIsViewPrepared = false;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (null != this.mRxManager) {
            this.mRxManager.clear();
            this.mRxManager = null;
        }
        if(null != mPresenter){
            if(!mPresenter.isDestroyed()){
                mPresenter.onDestroy();
            }
        }
        if (mLoadingDialog != null) {
            mLoadingDialog.hide();
            mLoadingDialog.destroy();
            mLoadingDialog = null;
        }
    }

    /**
     * 懒加载数据
     */
    private void lazyLoadData() {
        if (super.getUserVisibleHint() && !this.mHasLoadData && this.mIsViewPrepared) {
            this.initData();
            this.mHasLoadData = true;
        }
    }

    /**
     * 创建presenter
     *
     * @return
     */
    protected abstract P createPresenter();

    /**
     * 获取布局文件id
     *
     * @return
     */
    protected abstract int getContentViewId();

    /**
     * 初始化组件
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    /**
     * 初始化监听器
     */
    protected abstract void initListener();

    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {}

    @Override
    public void showLoading() {
        if (this.mLoadingDialog != null && !this.mLoadingDialog.isShowing()) {
            this.mLoadingDialog.show();
        }
    }

    @Override
    public void hideLoading() {
        if (this.mLoadingDialog != null && this.mLoadingDialog.isShowing()) {
            this.mLoadingDialog.hide();
        }
    }
}
